import {createRouter,createWebHashHistory} from  'vue-router'
import api from '../api'
const islogin = async function (to,from){
    //获取token
    let token = sessionStorage.getItem("token");
    //验证token是否有效
    let result = await api.get("api/checktoken");
    if(!token || result.code != 200){
        return {path:'/login'}
    }
}
const router=createRouter(
    {
    history:createWebHashHistory(),
    routes:[
        {path:"/",redirect:"/index"},
        {path:'/index',component:()=>import("../views/index.vue"),
            children:[{path:'/vehicles',component:()=>import("../views/vehicles.vue")},
                {path:'/stafflist',component:()=>import("../views/stafflist.vue")},
                {path:'/user',component:()=>import("../views/user.vue")},
                {path:'/customer',component:()=>import("../views/customer.vue")},
                {path:'/order',component:()=>import("../views/order.vue")},
                {path:'/returncheck',component:()=>import("../views/returncheck.vue")},
                {path:'/tenance',component:()=>import("../views/tenance.vue")},
                {path:'/brand',component:()=>import("../views/brand.vue")},
                {path:'/chart',component:()=>import("../views/chart.vue")},

            ],
            beforeEnter:islogin//当访问/index的时候，要先执行这个方法
        },
        {path:"/login",component:()=>import("../views/login.vue")},
        {path:'/editpass',component:()=>import("../views/editpass.vue")}


    ]
    }
);

export default router;